package com.ccys.common.third.payPal.config;

import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.OAuthTokenCredential;
import com.paypal.base.rest.PayPalRESTException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Configuration
@DependsOn("payPalConstants")
public class PaypalConfig {

    @Bean
    public static Map<String, String> paypalSdkConfig(){
        Map<String, String> sdkConfig = new HashMap<>();
        sdkConfig.put("mode", PayPalConstants.mode);
        return sdkConfig;
    }

    @Bean
    public OAuthTokenCredential authTokenCredential(){
        return new OAuthTokenCredential(PayPalConstants.clientId, PayPalConstants.clientSecret, paypalSdkConfig());
    }

    @Bean
    public APIContext apiContext() throws PayPalRESTException{
        //APIContext apiContext = new APIContext(authTokenCredential().getAccessToken());
        APIContext apiContext = new APIContext(PayPalConstants.clientId, PayPalConstants.clientSecret, PayPalConstants.mode);
        apiContext.setConfigurationMap(paypalSdkConfig());
        return apiContext;
    }
}
